package com.buobao.springmvc.controller;

import com.buobao.springmvc.entity.Form;
import com.buobao.springmvc.entity.User;
import com.buobao.springmvc.entity.UserInfo;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/mapping") //这个路径是指以下所有方法访问时都要添加这个路径,如：localhost:8080/mapping/g
public class UrlMappingController {

//    @GetMapping("/g")
    @RequestMapping(value = "/g",method = {RequestMethod.GET}) //这种方式和上面的GetMapping相似
    @ResponseBody
    public String getMapping(@RequestParam(value = "user_name",required = false) String name,Long id){ //这里的required表示是否必传，默认为true，当必传时没有传值则会报错；非必传时接收到的是null
        return "get name:"+name+"\n id:"+id;
    }

    @PostMapping(value="/p")
    @ResponseBody
    public String postMapping(String username,String password){
        return "post username:"+username+"\n password:"+password;
    }

    @PostMapping(value="/p1")
    @ResponseBody
    public String postMapping1(User user) { //实体接收参数时，前端参数名和实体中的字段名要严格相同
        return user.toString();
    }

    @PostMapping(value = "/p2") //这里produces用户处理响应中文字符串乱码
    @ResponseBody
    public String postMapping2(String name,String job, int[] purpose) {
        return "name:"+name+",job:"+job+",purpose:"+purpose;
    }

    @PostMapping(value = "/p3", produces = "application/json;charset=UTF-8") //这里produces用户处理响应中文字符串乱码
    @ResponseBody
    public String postMapping3(String name,String job, @RequestParam List<Integer> purpose) { //list接收需要用注解标识
        return "name:"+name+",job:"+job+",purpose:"+purpose;
    }

    @PostMapping(value = "/p4", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String postMapping4(Form form){
        return form.getName()+" "+form.getJob();
    }


    @PostMapping(value = "/p5", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String postMapping5(@RequestParam Map map){ //map在接收复合数据时有问题，比如list只能接受第一个item
        return map.toString();
    }


    //关联对象赋值
    @PostMapping(value = "/p6", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String postMapping6(UserInfo userInfo) {  //请查看form1.html中表单的传参方式
        return userInfo.toString();
    }

    //日期接收
    @PostMapping(value = "/p7", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String postMapping7(@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") Date date){  //如果传递的是entity则在具体的entity字段上面加这个注解也可以实现时间转换
        //如果不想每个时间参数上都添加这个注解也可以注册一个convertor来统一处理，这样就不用每个时间字段上都加这个注解的，详情看下一个示例
        return date.toString();
    }


    @PostMapping(value = "/p8", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String postMapping8(Date date){ //由于配置了转换器类，这里可以直接接收Date了
        return date.toString();
    }


    //关于ModelAndView的使用
    @GetMapping(value = "/mv")  //ModelAndView使用的是请求转发，如果要指定重定向可以使用 ModelAndView("redirect:/index.jsp")这种形式
    public ModelAndView showView() {
        User user = new User();
        user.setAge(10);
        user.setUsername("buobao");
        user.setPassword("1223334");

        ModelAndView modelAndView = new ModelAndView("/view.jsp");
        modelAndView.addObject("user",user);
        return modelAndView;
    }


    @GetMapping(value = "/mv1")
    public ModelAndView showView1() {
        User user = new User();
        user.setAge(10);
        user.setUsername("buobao");
        user.setPassword("1223334");

        ModelAndView modelAndView = new ModelAndView("redirect:/view.jsp");
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    //关于名称中的斜杠，如果不写斜杠的话，转发的页面需要在相应的路径下，这里就需要在webapp/mapping/下有一个对应的额页面了
    //这时候路径就依赖类中配置的@RequestMapping中的路径，不建议这么使用
    @GetMapping(value = "/mv2")
    public ModelAndView showView2() {
        User user = new User();
        user.setAge(10);
        user.setUsername("buobao");
        user.setPassword("1223334");

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user.jsp");
        modelAndView.addObject("user",user);
        return modelAndView;
    }


    //另一中更常用的形式！
    //ModelMap不是必须的，当直接放回string时（没有responseBody注解）时是跳转到对应的路径页面
    @GetMapping(value = "/mv3")
    public String showView3(Integer userId, ModelMap modelMap){
        String view = "/view.jsp";
        User user = new User();
        if (userId==1) {
            user.setUsername("Jack");
            user.setPassword("this is Jack");
        } else {
            user.setUsername("James");
            user.setPassword("this is James");
        }
        modelMap.addAttribute("user",user);
        return view;
    }

    //这个示例需要配置freemarker
    @GetMapping(value = "/mv4")
    public String showView4(Integer userId, ModelMap modelMap){
        if (userId==1) {
            return "/index";
        } else {
//            return "/mapping/g?user_name=Jack&id="+userId;
            User user = new User();
            user.setUsername("buobao");
            user.setPassword("123321");
            modelMap.addAttribute("u",user);
            return "/test";
        }
    }
}



























